﻿@model Nop.Plugin.Shipping.FixedOrByWeight.Models.ConfigurationModel
@inject Nop.Core.Domain.Common.AdminAreaSettings adminAreaSettings
@inject Nop.Services.Stores.IStoreService storeService
@{
    var stores = storeService.GetAllStores();
}

<form asp-controller="FixedOrByWeight" asp-action="Configure" method="post" id="shipping-byweight-form">
    <div class="panel-group advanced-setting">
        <div class="panel panel-default">
            <div class="panel-body">
                <p>
                    <b>@T("Plugins.Shipping.FixedOrByWeight.Formula")</b>
                    @T("Plugins.Shipping.FixedOrByWeight.Formula.Value")
                </p>
                <div id="shipping-byweight-grid"></div>
                <script type="text/javascript">
                    $(document)
                        .ready(function() {
                            $("#shipping-byweight-grid")
                                .kendoGrid({
                                    dataSource: {
                                        type: "json",
                                        transport: {
                                            read: {
                                                url: "@Html.Raw(Url.Action("RateByWeightList", "FixedOrByWeight"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: addAntiForgeryToken
                                            },
                                            destroy: {
                                                url: "@Html.Raw(Url.Action("DeleteRateByWeigh", "FixedOrByWeight"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: addAntiForgeryToken
                                            }
                                        },
                                        schema: {
                                            data: "Data",
                                            total: "Total",
                                            errors: "Errors",
                                            model: {
                                                id: "Id"
                                            }
                                        },
                                        requestEnd: function(e) {
                                            if (e.type == "update") {
                                                this.read();
                                            }
                                        },
                                        error: function(e) {
                                            display_kendoui_grid_error(e);
                                            // Cancel the changes
                                            this.cancelChanges();
                                        },
                                        pageSize: @(adminAreaSettings.DefaultGridPageSize),
                                        serverPaging: true,
                                        serverFiltering: true,
                                        serverSorting: true
                                    },
                                    pageable: {
                                        refresh: true,
                                        pageSizes: [@(adminAreaSettings.GridPageSizes)],
                                        @await Html.PartialAsync("~/Areas/Admin/Views/Shared/_GridPagerMessages.cshtml")
                                    },
                                    editable: {
                                        confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                        mode: "inline"
                                    },
                                    scrollable: false,
                                    columns: [
                                        {
                                            field: "StoreName",
                                            hidden: @(stores.Count > 1 ? "false" : "true"),
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.Store")",
                                            width: 200
                                        }, {
                                            field: "WarehouseName",
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.Warehouse")",
                                            width: 200
                                        }, {
                                            field: "CountryName",
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.Country")",
                                            width: 200
                                        }, {
                                            field: "StateProvinceName",
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.StateProvince")",
                                            width: 200
                                        }, {
                                            field: "Zip",
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.Zip")",
                                            width: 200
                                        }, {
                                            field: "ShippingMethodName",
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.ShippingMethod")",
                                            width: 200
                                        }, {
                                            field: "DataHtml",
                                            title: "@T("Plugins.Shipping.FixedOrByWeight.Fields.DataHtml")",
                                            width: 400,
                                            encoded: false
                                        }, {
                                            field: "Id",
                                            title: "@T("Admin.Common.Edit")",
                                            width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                            attributes: { style: "text-align:center" },
                                            template: "<button onclick=\"javascript:OpenWindow('@Url.Action("EditRateByWeighPopup", "FixedOrByWeight")?id=#=Id#&btnId=btnRefresh&formId=shipping-byweight-form', 800, 800, true); return false;\" class='btn btn-default'><i class=\"fa fa-pencil\"></i>@T("Admin.Common.Edit")</button>"
                                        }, {
                                            command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                                            title: "@T("Admin.Common.Delete")",
                                            width: 100
                                        }
                                    ]
                                });
                        });
                </script>
            </div>
            <div class="panel-footer">
                <button type="submit" id="btnAddNewRecord" class="btn btn-primary"
                        onclick="javascript:OpenWindow('@(Url.Action("AddRateByWeighPopup", "FixedOrByWeight", new { btnId = "btnRefresh", formId = "shipping-byweight-form" }))', 800, 800, true); return false;">
                    @T("Plugins.Shipping.FixedOrByWeight.AddRecord")
                </button>
                <input type="submit" id="btnRefresh" class="btn btn-default" style="display: none" />
            </div>
            <script type="text/javascript">
                $(document)
                    .ready(function () {
                        $('#btnRefresh')
                            .click(function () {
                                //refresh grid
                                var grid = $("#shipping-byweight-grid").data('kendoGrid');
                                //grid.dataSource.page(1); //new search. Set page size to 1
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                    });
            </script>
        </div>
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="form-group">
                    <div class="col-md-3">
                        <nop-label asp-for="LimitMethodsToCreated" />
                    </div>
                    <div class="col-md-9">
                        <nop-editor asp-for="LimitMethodsToCreated" />
                        <span asp-validation-for="LimitMethodsToCreated"></span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        &nbsp;
                    </div>
                    <div class="col-md-9">
                        <input type="button" id="savegeneralsettings" class="btn btn-primary" value="@T("Admin.Common.Save")" />

                        <script type="text/javascript">
                            $(document)
                                .ready(function() {
                                    $('#savegeneralsettings').click(function() {
                                        var postData = $(this.form).serialize();
                                        addAntiForgeryToken(postData);

                                        $.ajax({
                                            cache: false,
                                            type: 'POST',
                                            url: '@Url.Action("Configure", "FixedOrByWeight")',
                                            data: postData,
                                            dataType: 'json',
                                            success: function(data) {
                                                alert('Saved');
                                            },
                                            error: function(xhr, ajaxOptions, thrownError) {
                                                alert('Error while saving.');
                                            }
                                        });
                                        return false;
                                    });
                                });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>